<?php
/*
 * First authored by Brian Cray
 * License: http://creativecommons.org/licenses/by/3.0/
 * Contact the author at http://briancray.com/
 */
define('URLFZ',1);
require_once('config.php');
require_once(SOURCE_DIR . 'subs.php');
require_once(SOURCE_DIR . 'subs-shorten.php');
require_once(SOURCE_DIR . 'subs-sql.php');
require_once(SOURCE_DIR . 'recaptchalib.php');

/*
$error = "";

$url_to_shorten = trim($_REQUEST['longurl']);

// Add on http:// if no valid protocal exists
if(!preg_match(PROTOCALS, $url_to_shorten))
  $url_to_shorten = 'http://' . $url_to_shorten;

if(!empty($url_to_shorten))
{
checkBlacklist($url_to_shorten);
// check if the URL has already been shortened
	$already_shortened = alreadyShort($url_to_shorten);
	if(!empty($already_shortened))
	{
// URL has already been shortened
		$shortened_url = getShortenedURLFromID($already_shortened);
	}
// check if the URL is active
	else 
	{
//only verify Captcha if url is new. allow bots to retrieve existing urls
		verifyRecaptcha($_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
// URL not in database, insert
			checkURLexists($url);
				$shortened_url = shortenURL($url_to_shorten);
	}
	if(empty($error))
	  echo BASE_HREF . $shortened_url;
}else{
	$error = 'Invalad URL';
}
	echo $error;

/**/
//all methods die when encountering a fatal error.
$url = geturl();
checkBlacklist($url);
//antispam1();
$protocol = filterProtocol($url);
$domain = filterDomain($url);
$path = filterPath($url);
$query = filterQuery($url);
$url = $protocol . $domain . $path . $query;
//antispam2();
$nurl = alreadyShort($url);
if(epmty($nurl)){
	checkURLexists($url);
	$nurl = shortenURL($url);
}
return BASE_HREF . $nurl;

?>
